package com.unlcn.ils.kas.controller;

import com.unlcn.ils.kas.dao.model.sysadmin.Org;
import com.unlcn.ils.kas.dao.model.sysadmin.Role;
import com.unlcn.ils.kas.service.sysadmin.OrgService;
import com.unlcn.ils.kas.vo.ElTreeVO;
import com.unlcn.ils.kas.vo.RestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaoguixin on 2017/12/10.
 */
@Controller
@RequestMapping(value = "/org", produces = MediaType.APPLICATION_JSON_VALUE)
public class OrgController {

    @Autowired
    private OrgService orgService;

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public RestVO<List<ElTreeVO>> getOrgTree(){
        RestVO<List<ElTreeVO>> responseBO = new RestVO<>(0,"success");
        try{
            List<Org> orgList = orgService.listAllOrg();
            ElTreeVO elTreeVO = new ElTreeVO();
            elTreeVO.setId(0);
            elTreeVO.setLabel("组织机构");
            buildElTree(elTreeVO,orgList);
            List<ElTreeVO> elTreeVOList = new ArrayList<>();
            elTreeVOList.add(elTreeVO);
            responseBO.setData(elTreeVOList);
            return responseBO;
        }catch (Exception e){
            responseBO.setCode(1000);
            responseBO.setMessage("错误");
            return responseBO;
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public RestVO<List<Org>> getChildOrg(@RequestParam("parentId") Integer parentId){
        RestVO<List<Org>> responseBO = new RestVO<>(0,"success");
        try{
            List<Org> orgList = orgService.listOrgByPid(parentId);
            responseBO.setData(orgList);
            return responseBO;
        }catch (Exception e){
            responseBO.setCode(1000);
            responseBO.setMessage("错误");
            return responseBO;
        }
    }

    @GetMapping(value = "/listByUser")
    @ResponseBody
    //TODO 先去所有的，之后要加入权限判断，用户只能看到他所有部门以及该部门的子部门
    public RestVO<List<Org>> listByUser(){
        RestVO<List<Org>> responseBO = new RestVO<>(0,"success");
        try{
            List<Org> orgList = orgService.listAllOrg();
            responseBO.setData(orgList);
            return responseBO;
        }catch (Exception e){
            responseBO.setCode(30003);
            responseBO.setMessage("错误");
            return responseBO;
        }
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public RestVO<Org> createOrg(@RequestBody Org org){
        RestVO<Org> result = new RestVO<>(0,"success");
        try {
            int i = orgService.addOrg(org);
            if(i>0){
                result.setData(org);
                return result;
            }else{
                result.setCode(1000);
                result.setMessage("创建失败");
                return result;
            }
        }catch (Exception e){
            result.setCode(1000);
            result.setMessage(e.getMessage());
            return result;
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public RestVO<Org> updateOrg(@RequestBody Org org){
        RestVO<Org> result = new RestVO<>(0,"success");
        try {
            int i = orgService.updateOrg(org);
            if(i>0){
                result.setData(org);
                return result;
            }else{
                result.setCode(1000);
                result.setMessage("更新失败");
                return result;
            }
        }catch (Exception e){
            result.setCode(1000);
            result.setMessage(e.getMessage());
            return result;
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public RestVO<Role> deleteOrg(@RequestBody List<Integer> ids){
        RestVO result = new RestVO<>(0,"success");
        try {
            int i =  orgService.deleteOrg(ids);
            result.setData(i);
            return result;
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
            return result;
        }
    }

    private void buildElTree(ElTreeVO elTreeVO, List<Org> orgList) {
        List<ElTreeVO> children = new ArrayList<>();
        for (Org org : orgList) {
            if (org.getParent().equals(elTreeVO.getId())) {
                ElTreeVO childElTree = new ElTreeVO();
                childElTree.setId(org.getId());
                childElTree.setLabel(org.getName());
                buildElTree(childElTree,orgList);
                children.add(childElTree);
            }
        }
        elTreeVO.setChildren(children);
    }
}
